import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wanandroid_flutter/controller/project_page.dart';
import 'package:wanandroid_flutter/pages/web_page.dart';

///项目页面
class ProjectPage extends StatelessWidget {
  ProjectPage({super.key});

  final projectCtr = Get.put(ProjectController());

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      TabBar(
          controller: projectCtr.tabCtr,
          labelPadding:
              const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
          padding: const EdgeInsets.only(top: 35),
          isScrollable: true,
          tabAlignment: TabAlignment.start,
          tabs: projectCtr.tabs.map((element) {
            return Text(
              element.name.toString(),
              style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w700),
            );
          }).toList()),
      Expanded(
          child: TabBarView(
              controller: projectCtr.tabCtr,
              children: projectCtr.tabs.map((element) {
                return EasyRefresh(
                    //嵌套NestedScrollView的时候推荐使用这个MaterialHeader
                    controller: projectCtr.controller,
                    onRefresh: projectCtr.refreshRequest,
                    onLoad: projectCtr.loadMoreRequest,
                    child: Obx(() => GridView.builder(
                          padding: const EdgeInsets.only(top: 2),
                          itemCount: projectCtr.projectList.length,
                          itemBuilder: (context, index) {
                            var item = projectCtr.projectList[index];
                            return GestureDetector(
                                child: Card(
                                    color: Colors.white,
                                    clipBehavior: Clip.antiAlias,
                                    child: Stack(
                                        alignment: Alignment.bottomCenter,
                                        children: [
                                          Image.network(
                                            item.envelopePic,
                                            fit: BoxFit.fill,
                                            width: double.maxFinite,
                                            height: double.maxFinite,
                                          ),
                                          Container(
                                              width: double.infinity,
                                              alignment: Alignment.center,
                                              height: 50,
                                              padding:
                                                  const EdgeInsets.symmetric(
                                                      horizontal: 10),
                                              color: Get
                                                  .theme.colorScheme.primary
                                                  .withAlpha(180),
                                              child: Text(item.title,
                                                  style: const TextStyle(
                                                      color: Colors.white,
                                                      fontSize: 12),
                                                  maxLines: 2,
                                                  overflow:
                                                      TextOverflow.ellipsis))
                                        ])),
                                onTap: () {
                                  Get.to(() => WebPage(),
                                      arguments: {"webUrl": item.link});
                                });
                          },
                          gridDelegate:
                              const SliverGridDelegateWithFixedCrossAxisCount(
                                  crossAxisCount: 2, childAspectRatio: 0.5),
                        )));
              }).toList()))
    ]);
  }
}
